package com.etime.shycourse.controller.ad;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.pojo.AdPlace;
import com.etime.shycourse.services.ad.interfaces.AdPlacesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告位置管理
 */
@Api(tags = "后台-广告位置管理")
@RestController
@RequestMapping("/sys/adplace")
public class AdPlaceController {
    @Autowired
    private AdPlacesService adPlacesService;

    @ApiOperation("添加广告位置")
    @PostMapping("/insert")
    public BaseMessage insertAdPlace(AdPlace adplace){
        BaseMessage msg = new BaseMessage();
        Integer rtn=adPlacesService.insertAdPlace(adplace);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("广告位置添加成功");
        }else {
            msg.setMsg("广告位置添加失败");
        }

        return msg;

    }

    @ApiOperation("修改广告位置")
    @PostMapping("/update")
    public BaseMessage updateAdPlace(AdPlace adplace){
        BaseMessage msg = new BaseMessage();
        Integer rtn = adPlacesService.updataAdPlace(adplace);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("广告位置修改成功");
        }else {
            msg.setMsg("广告位置修改失败");
        }
        return msg;
    }

    @ApiOperation("查询广告位置")
    @GetMapping("/list")
    public ListMessage<AdPlace> selectAdplace(AdPlace adplace){
        ListMessage<AdPlace> msg = new ListMessage<AdPlace>();
        List<AdPlace> list=adPlacesService.selectAdplace(adplace);
        if(list!=null && list.size()>0){
            msg.setDatalist(list);
            msg.setCode(1);
            msg.setMsg("查询成功");
        }else{
            msg.setMsg("未查询到数据");
            msg.setCode(0);
        }
        return msg;
    }

    @ApiOperation("分页查询广告位置")
    @GetMapping("/page")
    public PageMessage<AdPlace> selectAdplacePage(AdPlace adplace,Long pageNum,Long pageSize){
        PageMessage<AdPlace> msg = new PageMessage<>();
        Page<AdPlace> page =adPlacesService.selectAdplacePage(adplace,pageNum,pageSize);
        if(page!=null){
            msg.setPagedata(page);
            msg.setMsg("查询成功");
            msg.setCode(1);
        }else {
            msg.setMsg("未查询到数据");
            msg.setCode(0);
        }
        return msg;
    }


    @ApiOperation("根据广告位id查询一条广告位置信息")
    @GetMapping("/get/{placeid}")
    public PojoMessage<AdPlace> selectAdPlaceById(@PathVariable Integer placeid ){
        PojoMessage<AdPlace> msg = new PojoMessage<>();
        AdPlace adplace = adPlacesService.selectAdplaceById(placeid);
        if(adplace!=null){
            msg.setData(adplace);
            msg.setMsg("查询成功");
        }else {
            msg.setMsg("查询失败");
        }
        return msg;

    }


}
